package com.jiezuo.auth.controller;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.jiezuo.auth.entity.User;
import com.jiezuo.auth.service.LoginService;
import com.jiezuo.common.base.BaseController;
//import com.jiezuo.common.sign.SignatureGeneration;
import com.jiezuo.common.util.MD5Util;
import com.jiezuo.common.util.ResultUtil;
import com.jiezuo.common.util.StringUtil;
import com.jiezuo.common.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 庞留杰
 * @version V1.0
 * @description 登录
 * @date 2020/7/17 14:21
 **/
@Slf4j
@RequestMapping("/")
@RestController
//登录管理", tags = {"登录管理接口"})
public class LoginController extends BaseController {
    //密码强度校验
    @Value("${weak.passwords}")
    private String weakPasswords;
    @Autowired
    private LoginService loginService;

    //登录接口", notes = "获取并返回用户信息及accessToken值")
    @PostMapping("/login")
    public Result login(@RequestBody User user, HttpServletRequest request) {
        return loginService.login(user, request);
    }

    //登出接口", notes = "登出接口")
    @GetMapping("/logout")
    public Result logout() {
        return loginService.logout(this.getLoginUser().getUser_name());
    }

    //三级菜单接口", notes = "获取当前用户三级菜单接口")
    //(name = "resource_type", value = "资源类型", dataType = "String", required = false, paramType = "query")
    @GetMapping("/threeLevelMenu")
    public Result threeLevelMenu(@RequestParam(value = "resource_type", required = false) String resource_type,
                                 @RequestParam(value = "belongs", required = false, defaultValue = "0") String belongs) {
        String user_id = this.getLoginUser().getId();
        return loginService.threeLevelMenu(user_id, resource_type, belongs);
    }

    //无限级菜单接口", notes = "获取当前用户无限级菜单接口")
    //(name = "resource_type", value = "资源类型", dataType = "String", required = false, paramType = "query")
    @GetMapping("/infiniteMenu")
    public Result infiniteMenu(@RequestParam(value = "resource_type", required = false) String resource_type,
                               @RequestParam(value = "is_all", required = false) String is_all,
                               @RequestParam(value = "belongs", required = false, defaultValue = "0") String belongs) {
        User user = this.getLoginUser();
        /* ****************************** 密码强度校验 start ***********************************/
        List<String> pds = StringUtil.isNotEmpty(weakPasswords) ? Arrays.asList(weakPasswords.split(",")) : Lists.newArrayList();
        for (String pd : pds) {
            String s = MD5Util.MD5(pd + user.getSalt());
            if (s.equals(user.getPassword())) {
                return new ResultUtil<JSONArray>().setData(new JSONArray(), "PASSWORD_WEAK");
            }
        }
        /* ****************************** 密码强度校验 end ***********************************/
        JSONArray jsonArray = loginService.infiniteMenu(user, resource_type, is_all, belongs);
        return new ResultUtil<JSONArray>().setData(jsonArray);
    }

    //登录历史接口【尚未开发】", notes = "登录历史接口【尚未开发】")
    @GetMapping("/loginHistory")
    public Result loginHistory() {
        return null;
    }

    /**
     * 功能描述:  鉴权接口
     *
     * @param accessToken
     * @param uri
     * @author: 庞留杰
     * @return: Result
     * @date: 2020/7/23 17:19
     */
    @GetMapping("/authentication")
    public Result authentication(String accessToken, String uri) {
        return loginService.authentication(accessToken, uri);
    }

    /**
     * 功能描述:  解析签名
     *
     * @param sign
     * @author: 庞留杰
     * @return: Result
     * @date: 2020/7/23 17:19
     */
     //用来屏蔽某些接口或参数
//    @GetMapping("/signature")
//    public Result signature(HttpServletRequest request, String sign) throws Exception {
//        return loginService.signature(request, sign);
//    }

    /**
     * 功能描述:  生成签名
     *
     * @param map
     * @author: 庞留杰
     * @return: Result
     * @date: 2020/7/23 17:19
     */
     //用来屏蔽某些接口或参数
//    @PostMapping("/sign")
//    public Result sign(@RequestBody Map<String, Object> map) {
//        String sign = SignatureGeneration.sign(map);
//        return new ResultUtil<>().setData(sign);
//    }

    //菜单接口", notes = "菜单接口")
    @GetMapping("/initMenu")
    public Result initMenu(@RequestParam(value = "resource_type", required = false) String resource_type,
                           @RequestParam(value = "is_all", required = false) String is_all) {
        JSONArray jsonArray = loginService.initMenu(this.getLoginUser(), resource_type, is_all);
        return new ResultUtil<JSONArray>().setData(jsonArray);
    }

}

